import React, { ReactNode } from "react";
import { Navigate, useLocation } from "react-router-dom";
import { useUserStore } from "../store/user";
type IGuardProps = {
  children: ReactNode;
};

export default function Guard(props: IGuardProps) {
  let { pathname } = useLocation();
  let { userInfo } = useUserStore((state) => state);
  const guard = () => {
    if (userInfo || pathname.indexOf("mine") == -1) {
      return props.children;
    } else {
      return <Navigate to={`/login?target=${pathname}`} />;
    }
  };
  return <>{guard()}</>;
}
